package com.cmf.myproject.logback;

import static org.fusesource.jansi.Ansi.ansi;

import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Color;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.CompositeConverter;

public class ColorByLevel extends CompositeConverter<ILoggingEvent> {

	@Override
	protected String transform(ILoggingEvent event, String in) {
		Ansi.setEnabled(true);
		Level level = event.getLevel();
		switch (level.toInt()) {
			case Level.ERROR_INT: {
				return ansi().fgBright(Color.RED).a(in).reset().toString();
			}
			case Level.WARN_INT: {
				return ansi().fg(Color.RED).a(in).reset().toString();
			}
			case Level.INFO_INT: {
				return ansi().fg(Color.GREEN).a(in).reset().toString();
			}
			case Level.DEBUG_INT:{
				return ansi().fgBright(Color.GREEN).a(in).reset().toString();
			}
			default: {
				return in;
			}
		}
	}

}
